home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / FIFO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-30  |  2.2 KB  |  117 lines

  1. #include "global.h"
  2. #ifdef FIFOSERVER
  3. #include "commands.h"
  4. #include "files.h"
  5. #include <fcntl.h>
  6. #include "hardware.h"
  7. #include "socket.h"
  8.  
  9. #ifndef O_SYNC
  10. #define O_SYNC 0
  11. #endif
  12.  
  13. int FIFOout = -1;
  14. static int FIFOin = -1;
  15. static int FIFOctl;
  16. static int FIFOtrace;
  17.  
  18.  
  19. extern int mkfifo (const char *__path, mode_t __mode);
  20. extern struct cmds Cmds[];
  21.  
  22.  
  23.  
  24. /* Start up FIFO server */
  25. int
  26. fifo1 (int argc, char *argv[], void *p OPTIONAL)
  27. {
  28. char buf[1024];
  29. int oldin, oldout;
  30. char *bptr;
  31. int offset = 0;
  32. char ch;
  33.  
  34.     if (argc > 1 && !strnicmp (argv[1], "trace", strlen(argv[1])))
  35.         FIFOtrace = 1;
  36.     else
  37.         FIFOtrace = 0;
  38.  
  39.     if (FIFOin != -1)    {
  40.         tprintf ("FIFO active: tracing o%s\n", (FIFOtrace) ? "n" : "ff");
  41.         return 0;
  42.     }
  43.     
  44.     (void) mkfifo (FIFO_IN, 0666);
  45.     if ((FIFOin = open (FIFO_IN, O_RDWR)) == -1)    {
  46.         tputs ("Can't open input FIFO");
  47.         return -1;
  48.     }
  49.     (void) mkfifo (FIFO_OUT, 0666);
  50.     if ((FIFOout = open (FIFO_OUT, O_RDWR | O_SYNC)) == -1)    {
  51.         tputs ("Can't open output FIFO");
  52.         close (FIFOin);
  53.         FIFOin = -1;
  54.         return -1;
  55.     }
  56.  
  57.     if (FIFOtrace)
  58.         tcmdprintf ("FIFO: started\n");
  59.     log (-1, "FIFO: started");
  60.  
  61.     oldout = Curproc->output;
  62.     oldin = Curproc->input;
  63.     Curproc->output = FIFOout;
  64.     Curproc->input = FIFOin;
  65.  
  66.     register_io (FIFOin, &FIFOctl);
  67.     while (FIFOin != -1)    {        
  68.         if (kwait (&FIFOctl) != 0)
  69.             continue;
  70.  
  71.         if (FIFOin == -1)
  72.             break;
  73.         (void) read (FIFOin, &ch, 1);
  74.         buf[offset++] = ch;
  75.         if (ch != '\n')
  76.             continue;
  77.  
  78.         buf[offset] = 0;
  79.         offset = 0;
  80.         if (FIFOtrace)
  81.             tcmdprintf ("FIFO: %s", buf);
  82.         log (-1, "FIFO: %s", buf);
  83.  
  84.         bptr = _variable_expansion (strdup (buf));
  85.         (void) cmdparse (Cmds, bptr, NULL);
  86.         write (FIFOout, "\nnet>\n", 6);
  87.         free (bptr);
  88.     }
  89.     if (FIFOtrace)
  90.         tcmdprintf ("FIFO: stopped\n");
  91.     log (-1, "FIFO: stopped");
  92.  
  93.     Curproc->output = oldout;
  94.     Curproc->input = oldin;
  95.     unlink (FIFO_IN);
  96.     unlink (FIFO_OUT);
  97.     return 0;
  98. }
  99.  
  100. /* Stop FIFO server */
  101. int
  102. fifo0 (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  103. {
  104.     if (FIFOin != -1)    {
  105.         unregister_io (FIFOin);
  106.         close (FIFOin);
  107.         close (FIFOout);
  108.         FIFOin = -1;
  109.         FIFOout = -1;
  110.         ksignal (&FIFOctl, 1);
  111.     }
  112.     return 0;
  113. }
  114.  
  115. #endif
  116.  
  117.